﻿using System;
using System.Collections.Generic;
using System.Web.UI;

namespace TSharp.Core.Web.Message.MessageImpl
{
    /// <summary>
    /// 情景实现类
    /// <para>by tangjingbo at 2009/12/14 21:37</para>
    /// </summary>
    internal class SituationImpl : ISituation
    {
        #region ISituation 成员

        private readonly Control senderObj;

        public SituationImpl(Control senderObj)
        {
            this.senderObj = senderObj;
        }

        public IMessageResult<TMessage> BroadcastMessage<TMessage>(TMessage message)
        {
            var tran = new MessageTransmitterImpl<TMessage>(senderObj, message);
            return HandleMassage(tran);
        }

        public IMessageResult<TMessage> BroadcastMessage<TMessage>(TMessage message, int maxDistance)
        {
            var tran = new MessageTransmitterImpl<TMessage>(senderObj, message);
            tran.MaxDistance = maxDistance;
            return HandleMassage(tran);
        }

        private IMessageResult<TMessage> HandleMassage<TMessage>(MessageTransmitterImpl<TMessage> tran)
        {
            List<IMessageListenerRegHandle<TMessage>> handles = MessageListenerManage.GetHandlers<TMessage>();
            if (handles != null)
            {
                for (int i = 0, n = handles.Count; i < n; i++)
                {
                    IMessageListenerRegHandle<TMessage> handle = handles[i];
                    tran.RegHandle = handle;
                    if (tran.IsTerminated)
                        break;

                    if (handle.Registered && handle.Enabled)
                    {
                        handle.GetListener()(this, tran.GetMessage(), tran);
                        tran.GetListeneds++;
                    }
                }
            }
            return tran;
        }

        #endregion

        #region 消息监听注册

        public IMessageListenerRegHandle<TMessage> RegMessageListener<TMessage>(MessageListener<TMessage> listener,
                                                                                bool autoUnReg, int order)
        {
            var handle = new MessageListenerRegHandleImpl<TMessage>(listener) {Order = order};
            if (autoUnReg)
            {
                senderObj.Unload += (o, e) => handle.UnRegister();
            }
            return handle;
        }

        public IMessageListenerRegHandle<TMessage> RegMessageListener<TMessage>(MessageListener<TMessage> listener)
        {
            return RegMessageListener(listener, true, int.MaxValue);
        }

        public IMessageListenerRegHandle<TMessage> RegMessageListener<TMessage>(MessageListener<TMessage> listener,
                                                                                int order)
        {
            return RegMessageListener(listener, true, order);
        }

        public IMessageListenerRegHandle<TMessage> RegMessageListener<TMessage>(MessageListener<TMessage> listener,
                                                                                bool autoUnReg)
        {
            return RegMessageListener(listener, true, int.MaxValue);
        }

        #endregion
    }
}